package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configIDCardRouter(router *httprouter.Router) {
	router.GET("/idcard", GetAllIDCard)
	router.POST("/idcard", AddIDCard)
	router.GET("/idcard/:argType/:argId", GetIDCard)
	router.PUT("/idcard/:argType/:argId", UpdateIDCard)
	router.DELETE("/idcard/:argType/:argId", DeleteIDCard)
}

func configGinIDCardRouter(router gin.IRoutes) {
	router.GET("/idcard", ConverHttprouterToGin(GetAllIDCard))
	router.POST("/idcard", ConverHttprouterToGin(AddIDCard))
	router.GET("/idcard/:argType/:argId", ConverHttprouterToGin(GetIDCard))
	router.PUT("/idcard/:argType/:argId", ConverHttprouterToGin(UpdateIDCard))
	router.DELETE("/idcard/:argType/:argId", ConverHttprouterToGin(DeleteIDCard))
}

// GetAllIDCard is a function to get a slice of record(s) from id_card table in the custom_process database
// @Summary Get list of IDCard
// @Tags IDCard
// @Description GetAllIDCard is a handler to get a slice of record(s) from id_card table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.IDCard}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /idcard [get]
// http "http://localhost:8080/idcard?page=0&pagesize=20" X-Api-User:user123
func GetAllIDCard(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "id_card", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllIDCard(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetIDCard is a function to get a single record from the id_card table in the custom_process database
// @Summary Get record from table IDCard by  argType  argId
// @Tags IDCard
// @ID argType
// @ID argId
// @Description GetIDCard is a function to get a single record from the id_card table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argType path int true "type"
// @Param  argId path int64 true "id"
// @Success 200 {object} model.IDCard
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /idcard/{argType}/{argId} [get]
// http "http://localhost:8080/idcard/1/1" X-Api-User:user123
func GetIDCard(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argType, err := parseUint32(ps, "argType")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetIDCard(ctx, argType, argId)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddIDCard add to add a single record to id_card table in the custom_process database
// @Summary Add an record to id_card table
// @Description add to add a single record to id_card table in the custom_process database
// @Tags IDCard
// @Accept  json
// @Produce  json
// @Param IDCard body model.IDCard true "Add IDCard"
// @Success 200 {object} model.IDCard
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /idcard [post]
// echo '{"type": 89,"id": 53,"id_1": "FFdGjQJLVdWcWXqCSKOwDWDdJ","id_5": "KfVXfLfbmcEgpTscAuoLaUGuh","id_6": "fyLJfgDfDbvZLnqEhGuaFqDdl","id_7": "bSOGCkHyCfMLMGuxVNIglCdED","create_time": "2075-05-13T21:29:03.161760643+08:00","update_time": "2260-04-11T02:35:19.322715944+08:00","id_2": "mPDHFjHuTsfXPQBIeNJqhPhoI","id_3": "rVXkSXBomDLIghbaxaTAGHMpS","id_4": "TICVEKetDAEpvEOgTEOEVxUGm","id_8": "PwkrbCntpShTEmhfgWVpxaDcq"}' | http POST "http://localhost:8080/idcard" X-Api-User:user123
func AddIDCard(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	idcard := &model.IDCard{}

	if err := readJSON(r, idcard); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := idcard.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	idcard.Prepare()

	if err := idcard.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	idcard, _, err = dao.AddIDCard(ctx, idcard)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, idcard)
}

// UpdateIDCard Update a single record from id_card table in the custom_process database
// @Summary Update an record in table id_card
// @Description Update a single record from id_card table in the custom_process database
// @Tags IDCard
// @Accept  json
// @Produce  json
// @Param  argType path int true "type"// @Param  argId path int64 true "id"
// @Param  IDCard body model.IDCard true "Update IDCard record"
// @Success 200 {object} model.IDCard
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /idcard/{argType}/{argId} [put]
// echo '{"type": 89,"id": 53,"id_1": "FFdGjQJLVdWcWXqCSKOwDWDdJ","id_5": "KfVXfLfbmcEgpTscAuoLaUGuh","id_6": "fyLJfgDfDbvZLnqEhGuaFqDdl","id_7": "bSOGCkHyCfMLMGuxVNIglCdED","create_time": "2075-05-13T21:29:03.161760643+08:00","update_time": "2260-04-11T02:35:19.322715944+08:00","id_2": "mPDHFjHuTsfXPQBIeNJqhPhoI","id_3": "rVXkSXBomDLIghbaxaTAGHMpS","id_4": "TICVEKetDAEpvEOgTEOEVxUGm","id_8": "PwkrbCntpShTEmhfgWVpxaDcq"}' | http PUT "http://localhost:8080/idcard/1/1"  X-Api-User:user123
func UpdateIDCard(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argType, err := parseUint32(ps, "argType")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	idcard := &model.IDCard{}
	if err := readJSON(r, idcard); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := idcard.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	idcard.Prepare()

	if err := idcard.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	idcard, _, err = dao.UpdateIDCard(ctx,
		argType, argId,
		idcard)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, idcard)
}

// DeleteIDCard Delete a single record from id_card table in the custom_process database
// @Summary Delete a record from id_card
// @Description Delete a single record from id_card table in the custom_process database
// @Tags IDCard
// @Accept  json
// @Produce  json
// @Param  argType path int true "type"// @Param  argId path int64 true "id"
// @Success 204 {object} model.IDCard
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /idcard/{argType}/{argId} [delete]
// http DELETE "http://localhost:8080/idcard/1/1" X-Api-User:user123
func DeleteIDCard(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argType, err := parseUint32(ps, "argType")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "id_card", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteIDCard(ctx, argType, argId)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
